<template>
	<view v-show="i === index">
		<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" height="100%" bottom="90" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback">
			<view class="data-list" v-for="(item, key) in dataList" :key="key" @click="toInfo(item)" @longpress="onLongPress(item)">
				<view class="data-item">
					<view class="item-text item-more">
						<view class="item-level">
							<view class="level">{{item.dfLevelNo | codeFormat(levelOptions)}}</view>
						</view>
						<view class="item-title">
							{{item.manageObjName}}/{{item.trueLocaName}}
						</view>
						<view class="item-states">
							{{item.state | codeFormat(stateOptions)}}
						</view>
					</view>
					<view class="item-text">
						<view class="item-desc">{{item.dfDesp}}</view>
					</view>
					<view class="item-text item-desc item-more">
						<view class="">{{item.dfType | codeFormat(typeOptions)}}</view>
						<view class="">{{item.skillNo | codeFormat(majorOptions)}}</view>
						<view class="">{{item.finddate | dateFormat('yyyy-MM-dd HH:mm')}}</view>
					</view>
				</view>
			</view>
		</mescroll-uni>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js"
	import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js"
	import { getDefectList, getDefectLevel, deleteDefect } from '@/api/business/defect.js'
	import { getPendingTasks } from '@/api/base/flow.js'
	import { findByTypeKeyExtend } from '@/api/platform/common.js'
	
	export default {
		mixins: [
			MescrollMixin,
			MescrollMoreItemMixin
		],
		props:{
			i: Number,
			index: { 
				type: Number,
				default(){
					return 0
				}
			},
			params: {
				type: Object,
				default(){
					return {}
				}
			}
		},
		data() {
			return {
				downOption:{
					auto:false
				},
				upOption:{
					auto:false
				},
				dataList: [] ,
				czOptions: [],
				stateOptions: [],
				levelOptions: [],
				typeOptions: [],
				majorOptions: [],
				flowList: []
			}
		},
		watch: {
			params: {  
				handler(newValue, oldValue) {
					if(newValue.type == "query") {
						this.reload()
					}
				},
				deep: true,
				immediate: false
			}
		},
		mounted() {
			// this.init()
		},
		methods: {
			init() {
				uni.getStorageSync("stationList").forEach(item=>{
					this.czOptions.push({
						value: item.id,
						label: item.name
					})
				})
				
				getDefectLevel().then(res=>{
					for (const [key, value] of Object.entries(res.data)) {
						this.levelOptions.push({
							value: key,
							label: value
						})
					}
				})
				
				// 数据字典（专业，状态，缺陷类型）
				findByTypeKeyExtend("zybm,qxywzt,qxlx").then(res=>{
					if(res.data) {
						if(res.data.zybm) {
							res.data.zybm.forEach(item=>{
								this.majorOptions.push({
									value: item.key,
									label: item.name
								})
							})
						}
						
						if(res.data.qxywzt) {
							res.data.qxywzt.forEach(item=>{
								this.stateOptions.push({
									value: item.key,
									label: item.name
								})
							})
						}
						
						if(res.data.qxlx) {
							res.data.qxlx.forEach(item=>{
								this.typeOptions.push({
									value: item.key,
									label: item.name
								})
							})
						}
					}
				})
			},
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			async upCallback(page) {
				this.init()
				
				let pageNum = page.num // 页码, 默认从1开始
				let pageSize = page.size // 页长, 默认每页10条
				let pager = {
					start: page.num,
					limit: page.size
				}
				
				if(this.index == 0){ // 待我处理
					// 待办数据
					let ids = []
					await getPendingTasks({
						start: 1,
						limit: 1000
					}).then(res => {
						if (res.data.dataResult.length > 0) {
							let processKey = ''
							let projectType = uni.getStorageSync("projectType")
							if(projectType == 'wind') {
								processKey = "Process_defect_eqpDefect_V1"
							} else if(projectType == 'fire') {
								processKey = "DfFireProcess"
							}
							
							res.data.dataResult.forEach(item => {
								if (item.procDefKey == processKey) {
									ids.push(item.instBizKey)
								}
							})
						}
					})
					
					if(ids.length === 0) {
						ids.push("-1")
					}
					this.params.dfIds = ids.join()
				}else if(this.index == 1){ // 我创建的
					this.params.dfIds = null
					this.params.createUser = uni.getStorageSync("userId")
				}else if(this.index == 2){ // 全部
					this.params.dfIds = null
					this.params.createUser = null
				}
				
				let projectType = uni.getStorageSync("projectType")
				if(projectType == 'fire') {
					this.params.dfClassification = "fire"
				}
				
				getDefectList(this.params, pager).then(res =>{
					let curPageData = res.data.records
					let curPageLen = curPageData.length
					let totalSize = res.data.total
					if(page.num == 1){
						//如果是第一页需手动置空列表
						this.dataList = []
					}
					for(let data of curPageData) {
						data.trueLocaName = data.locaName.substr(data.locaName.lastIndexOf('/')+1, data.locaName.length)
					}
					this.dataList = this.dataList.concat(curPageData) //追加新数据
					this.mescroll.endBySize(curPageLen, totalSize) 
				}).catch(()=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			toInfo(item) {
				let type = "view"
				if(this.index == 0) {
					type = "approve"
				} else if(this.index == 1 && item.state == "draft") {
					type = "edit"
				}
				
				uni.navigateTo({
					url: "/pages/business/defect/detail?dfId=" + item.dfId + "&type=" + type
				})
			},
			// 长按删除
			onLongPress(item) {
				if(this.index != 1 || item.state != 'draft') {
					return
				}
				let that = this
				setTimeout(() => {
					uni.showModal({
						title: that.$t('baseCommon.message.tips'),
						content: that.$t('defect.message.sureDelSelDef'),
						success:async function(res) {
							if (res.confirm) {
								deleteDefect({ dfId: item.dfId })
								setTimeout(()=>{
									that.reload()
								}, 100)
							} else if (res.cancel) {
								
							}
						}
					})
				}, 500);
			},
			reload(){
				if(this.i === this.index) {
					this.mescroll.resetUpScroll()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/style/common-list.scss';
	
	.data-list {
		padding: 8px;
	}
	
	.item-level {
		margin-right: 8px;
		
		.level {
			width: 40px;
			height: 40px;
			color: #FFFFFF;
			text-align: center;
			line-height: 40px;
			font-size: 12px;
			background: url(../../../../static/images/icon/8.png);
			background-size: 100% 100%;
		}
	}
	
	.item-states {
		margin-left: 5px;
		min-width: 60px;
		color: $uni-color-primary;
		text-align: right;
	}
	
	.item-title {
		min-width: calc(100% - 115px);
	}
	
	.item-more {
		align-items: center;
		justify-content: space-between;
	}
</style>